<template>
	<view class="zai-box">
		<scroll-view scroll-y class="page">
			<!-- 登录页文字展示 -->
			<!-- <view class="text-center" :style="[{animation: 'show ' + 0.4+ 's 1'}]"> -->
			<view class="text-center">
				<view class="zai-title text-shadow">Pe plus loan </view>
				<view class="zai-title text-shadow">welcome </view>
			</view>
			<!-- 登录页登陆 -->
			<view class="login">
				<image src="../../static/log.png" mode=""></image>
				<view class="login-count">
					<text class="log_title">Sign in</text>
					<!-- 登陆输入框 -->
					<u--form labelPosition="left" :model="model1" ref="uForm" :rules="rules">
						<u-form-item prop="userInfo.iphone" borderBottom ref="item1">
							<image src="../../static/login/login_iphone.png" mode=""
								style="width: 36rpx; height: 40rpx; margin-right: 12rpx"></image>
							<view class="quhao" style="
                  font-size: 30rpx;
                  color: #333;
                  font-weight: 500;
                  margin-right: 10px;
                ">
								+91
							</view>
							<u--input v-model="model1.userInfo.iphone" border="none" placeholder="Enter phone number"
								placeholderStyle="color:#000"></u--input>
						</u-form-item>
						<u-form-item v-show="showse" prop="userInfo.code" borderBottom ref="item1" class="from_item2">
							<image src="../../static/login/login_yzm.png" mode=""
								style="width: 36rpx; height: 40rpx; margin-right: 30rpx"></image>
							<u--input border="none" maxlength="7" placeholder="Enter Otp" placeholderStyle="color:#000"
								v-model="value">
								<u-code ref="uCode" @change="codeChange" seconds="20" changeText="Xs Enter Otp"
									startText="Enter Otp" endText="reacquire">
								</u-code>
							</u--input>
							<button @click="getCode" size="mini"
								style="font-size: 30rpx;color: :#000;font-weight: 400 ;border: 0px; background: none;">
								Enter Otp
							</button>
						</u-form-item>
						<u-button @click="submit" class="logins">LOGIN</u-button>
					</u--form>
					<view class="logon_fu">
						<text>
							The next step means that you agree to our user registration
						</text>
					</view>
					<view class="logon_foot">
						<radio :checked="showes" @click="changes" class="radius"></radio><text>Login means that you have read and agreed to the
							<u-link href="https://universal.kwbkqi.top/gw/fustsmartloanys.html"
								style="display: inline-block; margin-left: 10rpx" text=" 'Privacy Policy'"
								color="#9F9ABC" line-color="#9F9ABC"></u-link>
						</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 登录加载弹窗 -->
		<u-modal showCancelButton :show="shows" disabled="true" confirmText="Agree" closeOnClickOverlay
			cancelText="Do no use" @close="shows = true" cancelColor="#8C8D8E" @confirm="next" @cancel="close">
			<view class="slot-content">
				<view class="">
					Please be sure to carefully read and fully understand the terms of the
					“Privacy Policy”. If you agree, please click “Agree“ to start
					accepting our services
					<u-link href="https://universal.kwbkqi.top/gw/fustsmartloanys.html" text="Open privacy file"
						color="#9F9ABC" line-color="#9F9ABC"></u-link>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import configService from "@/common/service/config.service.js";
	import {
		getRawKey,
		getEncryptString,
		getDecryptString
	} from "../../defaut.js";
	import CryptoJS from "crypto-js";
	import $_storage from "../../common/util/storage";
	import {
		encoding
	} from "../../common/util/common";
	export default {
		data() {
			return {
				types: {
					types: "",
				},
				font: "",
				front3: "",
				shape: "", //round 圆形
				loading: false,
				phoneNo: "",
				smsCode: "",
				showPassword: false, //是否显示明文
				loginWay: 1, //1: 账密，2：验证码
				smsCountDown: 0,
				smsCountInterval: null,
				toggleDelay: false,
				version: "",
				//第三方登录相关信息
				thirdType: "",
				thirdLoginInfo: "",
				thirdLoginState: false,
				bindingPhoneModal: false,
				thirdUserUuid: "",
				model1: {
					userInfo: {
						iphone: "",
					},
				},
				tips: "Enter Otp",
				value: "",
				password: "universa",
				encoded: "",
				code: "",
				shows: false, //验证码
				showes: true,
				register: false,
				rules: {
					"model1.userInfo.iphone": {
						type: "tring",
						require: true,
						message: "Please enter your phone number",
						trigger: ["blur", "change"],
					},
					value: {
						type: "tring",
						require: true,
						message: "Please enter the verification code",
						trigger: ["blur", "change"],
					},
				},
				showse: false, //是否显示验证码
				zhijie: '', //是否直接登录
				nativePlug:null, //打点
			};
		},
		onLoad: function() {
			this.pop();
			this.peizhi();
			uni.getSystemInfo({
				success: function(res) {
					uni.setStorageSync("brand", res.brand);
				},
			});
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			// 验证用户手机号是否注册
			registers() {
				const params = {
					phone: this.model1.userInfo.iphone,
				};
				const paramsString = JSON.stringify(params);
				this.encoded = getEncryptString(paramsString, this.$password());
				this.$http
					.get("/user/v1/checkphoneisreg", {
						params: {
							login: this.encoded,
						},
					})
					.then((res) => {
						if (res.data.status == 200) {
							this.register = getDecryptString(res.data.data, this.$password());
							// 存储用户是否注册  未注册需要打点
							uni.setStorageSync("register", this.register);
							if(this.register=='false'){
								this.nativePlug = uni.requireNativePlugin("FbKochava")
								if(this.nativePlug != null){
									this.nativePlug.eventTracking("reg_tel","tel",this.model1.userInfo.iphone)
								}
							}
						}
					});
			},
			// 获取系统配置信息
			peizhi() {
				this.$http.get("/sysconfig/list1").then((res) => {
					if (res.data.status == 200) {
						// console.log(getDecryptString(res.data.data, this.$password()));
						let arr = JSON.parse(getDecryptString(res.data.data, this.$password()));
						console.log(arr)
						this.font = arr.filter((el) => el.sysKey == "loan_mode");
						// this.front3 = arr.filter((el) => el.sysVal == "front3_2");
						let fonts = arr.filter((el) => el.sysKey == "短信是否显示");
						let xitong = arr.filter((el) => el.sysKey == "phone");
						uni.setStorageSync("xitong", xitong.sysVal);
						if (fonts[0].sysVal == "是") {
							this.showse = true;
						} else {
							this.showse = false;
						}
						let shenhe = arr.filter((el) => el.sysKey == "审核等待页面");
						if (shenhe[0].sysVal == "是" && uni.getStorageSync("token")) {
							this.$Router.replaceAll({
								name: "backupcopy",
							});
						}
						// 是否直接登录
						let lo = arr.filter((el) => el.sysKey == "是否直接登录");
						if (lo[0].sysVal == "是") {
							this.zhijie = true;
						} else {
							this.zhijie = false;
						}
					}
				});
			},
			// 获取验证码  且账号必须十位数字
			getCode() {
				if(this.showes != true){
					uni.showToast({
						title: "Please check the agreement below",
						icon: "none",
						duration: 2000,
					});
					return;
				}
				// 手机号填写了 并且 十位 并且不等于谷歌账号并且验证码显示  则获取验证码
				if (
					this.model1.userInfo.iphone &&
					this.model1.userInfo.iphone.length == 10 &&
					this.model1.userInfo.iphone != 9923612553 && this.showse
				) {
					this.registers();
					this.$http
						.get("/user/code/sms", {
							params: {
								login: this.encoded,
							},
						})
						.then((res) => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							// this.$tip.success("The verification code has been sent");
							setTimeout(() => {
								uni.showToast({
									title: "The verification code has been sent",
									icon: "none",
									duration: 2000,
								});
							}, 0);
							// 通知验证码组件内部开始倒计时
							// this.$refs.uCode.start();
							if (res.data.status == 200) {
								this.code = getDecryptString(res.data.data, this.$password());
								this.value = this.code;
								// this.$tip.success("Sent successfully!");
								// 获取验证码直接登录 系统配置 直接登录
								if (this.zhijie==true) {
									setTimeout(()=>{
										this.submit();
									},500)
									
								}
								setTimeout(() => {
									uni.showToast({
										title: "Sent successfully!",
										icon: "none",
										duration: 1500,
									});
								}, 2000);

								// uni.showToast({
								// 	title: 'The verification code has been sent',
								// 	icon: 'none',
								// 	duration: 2000,
								// });
							}
						});
					// 这里被我注释掉了  不然报错找不到页面
					// if (this.$refs.uCode.canGetCode) {
					// 	uni.showLoading({
					// 		title: 'Getting the verification code'
					// 	})
					// 	const params = {
					// 		phone: this.model1.userInfo.iphone
					// 	};
					// 	const paramsString = JSON.stringify(params);
					// 	this.encoded = getEncryptString(paramsString, this.$password())
					// 	// console.log(this.encoded)
					// 	// 将加密编码后的数据作为login参数传递给请求
					// 	this.$http.get("/user/code/sms", {
					// 		params: {
					// 			login: this.encoded
					// 		}
					// 	}).then(res => {
					// 		uni.hideLoading();
					// 		// 这里此提示会被this.start()方法中的提示覆盖
					// 		uni.$u.toast('The verification code has been sent');
					// 		// 通知验证码组件内部开始倒计时
					// 		this.$refs.uCode.start();
					// 		if (res.data.status == 200) {
					// 			this.code = getDecryptString(res.data.data, this.$password())
					// 			console.log(this.code);
					// 			this.value = this.code
					// 			this.$tip.success('Sent successfully!');
					// 		}
					// 	})
					// } else {
					// 	uni.$u.toast('Wait until the countdown is over');
					// }
					// ...
					// ((this.model1.userInfo.iphone == 9923612553 && !this.value) || (!this.showse && !this.value))
					
					// || !this.shows  或者验证码不显示 或者 验证码不显示 
				} else if (this.model1.userInfo.iphone == 9923612553  ||(this.zhijie == true && this.model1.userInfo.iphone.length == 10)) {
					// 如果输入的账号为谷歌人员测试账号    或者 可以直接登录  则直接登录
					this.registers();
					this.$http
						.get("/user/code/sms", {
							params: {
								login: this.encoded,
							},
						})
						.then((res) => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.showToast({
								title: "The verification code has been sent",
								icon: "none",
								duration: 2000,
							});
							// 通知验证码组件内部开始倒计时
							// this.$refs.uCode.start();
							if (res.data.status == 200) {
								this.code = getDecryptString(res.data.data, this.$password());
								this.value = this.code;
								this.$tip.success("Sent successfully!");
								setTimeout(() => {
									this.submit();
								}, 300);

							}
						});
				} else {
					uni.showToast({
						title: "Please enter the correct phone number (only 10)",
						icon: "none",
						duration: 2000,
					});
				}
			},
			submit() {
				if(this.showes != true){
					uni.showToast({
						title: "Please check the agreement below",
						icon: "none",
						duration: 2000,
					});
					return;
				}
				if (!this.model1.userInfo.iphone) {
					// 未填写 手机号
					uni.showToast({
						title: "Please enter your phone number",
						icon: "none",
						duration: 2000,
					});
					return;
				} else if (!this.value && this.showse==true && this.model1.userInfo.iphone!=9923612553) {
					//  未填写 验证码  并且 验证码显示
					uni.showToast({
						title: "Please enter the verification code",
						icon: "none",
						duration: 2000,
					});
					return;
				} else if (!this.value && !this.showse && this.zhijie == true) {
					// 未填写 验证码  并且 验证码不显示  则直接去获取验证码
					console.log(123)
					this.getCode();
					return;
				}else if(!this.value&&this.model1.userInfo.iphone==9923612553){
					this.getCode();
					return;
				}
				
				
				// 都满足  则发起登录请求
				uni.showLoading({
					title: "Logging in...",
				});
				const params = {
					phone: this.model1.userInfo.iphone,
					verCode: this.value,
				};
				const paramsString = JSON.stringify(params);
				this.encoded = getEncryptString(paramsString, this.$password());
				// 将加密编码后的数据作为login参数传递给请求
				this.$http
					.get("/user/login", {
						params: {
							login: this.encoded,
						},
					})
					.then((res) => {
						console.log(res)
						if (res.data.status == 200) {
							var token = getDecryptString(res.data.data, this.$password());
							uni.setStorageSync("token", token);
							uni.setStorageSync("phone", this.model1.userInfo.iphone);
							uni.showToast({
								title: "Successful landing!",
								icon: "none",
								duration: 2000,
							});

							this.$http.get("/user/v1/getcurruser", {}).then((res) => {
								let info = getDecryptString(res.data.data, this.$password());
								// 根据要判断 infoAuthStatus 的值跳转不同的 页面   0:未认证 1:已认证 2:审核中 3：开始认证
								uni.setStorageSync("userInfo", info);
								if (!uni.getStorageSync("stringcode"))
									uni.setStorageSync("stringcode", encoding());
								this.types.types = JSON.parse(info).infoAuthStatus;
								uni.setStorageSync('types', JSON.parse(info).infoAuthStatus)
								  // console.log('info', info)
								  // console.log('this.font.sysVal',this.font.sysVal)
								  // console.log(JSON.parse(info).infoAuthStatus)
								  // console.log(this.font[0].sysVal)
								  // console.log(JSON.parse(info).loanAmount)
								if (JSON.parse(info).infoAuthStatus != 1) {
									uni.reLaunch({
									 url: '/pages/home/home' 
									});
									// this.$Router.replaceAll({
									// 	name: "home",
									// 	params: this.types,
									// });
								} else if (
									JSON.parse(info).infoAuthStatus == 1 && !JSON.parse(info).loanAmount
								) {
									
									// &&JSON.parse(info).loanAmount.length == 0
									// 跳转第十个页面
									this.$Router.replaceAll({
										name: "huxing",
										params: this.types,
									});
									
								} else if (
									JSON.parse(info).loanAmount &&
									JSON.parse(info).loanAmount.length > 0 &&
									this.font[0].sysVal=='front3'
								) {
									// &&且模式==front3
									// 跳转第十二个页面
									this.$Router.replaceAll({
										name: "index",
										params: this.types,
									});
								} else if (
								// 这个有问题 跳不过去
									JSON.parse(info).loanAmount &&
									this.font[0].sysVal=='front3_2'
								) {
									this.$Router.replaceAll({
										name: "audithome",
										params: this.types,
									});
								}
								// else if((Date.parse(new Date()) - uni.getStorageSync('times'))/1000 < 180 ){
								// 	this.$Router.replaceAll({
								// 		name: "backupcopy",
								// 	});
									
								// }
								// else{
								// 	this.$Router.replaceAll({
								// 		name: "index",
								// 	});
								// }
							});
						} else {
							uni.hideLoading(); // Stop the loading message if there is an error with the login request
							uni.$u.toast("Network error. Please try again later.");
						}
					});
			},
			next() {
				// this.shows=true
				this.showes = true;
				this.shows = false;
				uni.setStorageSync("pops", 1);
			},
			// 判断 刚进来登录页 是否要 打开弹窗
			pop() {
				if (uni.getStorageSync("pops")) {
					 this.shows = false;
				} else {
					 this.shows = true;
				}
			},
			close() {
				this.shows = false;
				// #ifdef APP-PLUS
				uni.removeStorageSync("token");
				if (plus.os.name.toLowerCase() === "android") {
					plus.runtime.quit();
				} else {
					const threadClass = plus.ios.importClass("NSThread"); // 导入了iOS的 NSThread 类。
					const mainThread = plus.ios.invoke(threadClass,
						"mainThread"); // 通过调用 NSThread 类的 mainThread 方法获取主线程对象。
					plus.ios.invoke(mainThread, "exit"); //调用主线程对象的 exit 方法，退出应用程序。
					// ios11
					plus.ios
						.import("UIApplication")
						.sharedApplication()
						.performSelector(
							"exit"
							); //导入 UIApplication 类，调用 sharedApplication 方法获取应用程序对象，然后使用 performSelector 方法调用应用程序对象的 exit 方法,  针对iOS 11及以上版本的退出方式
				}
				//   #endif
				// #ifdef APP-PLUS
				uni.exit({
					success: function() {
						console.log("应用程序成功退出");
					},
					fail: function(err) {
						console.log("退出应用程序失败:", err);
					},
				});
				// #endif
			},
			changes() {
				this.showes = !this.showes;
			},
		},
		beforeDestroy() {
			if (this.smsCountInterval) {
				clearInterval(this.smsCountInterval);
			}
		},
		onBackPress() {
			 //#ifdef APP-PLUS
			 console.log(111)
			      uni.exitMiniProgram(); // 退出应用程序
			      //#endif
				  
			// #ifdef APP-PLUS
			console.log(111)
			uni.exit({
				success: function() {
					console.log("应用程序成功退出");
				},
				fail: function(err) {
					console.log("退出应用程序失败:", err);
				},
			});
			// #endif
			return true;
		},
	};
</script>

<style scoped>
	page {
		background-color: #d53030;
		height: 100%;
		box-sizing: border-box;
	}

	.zai-box {
		height: 100%;
		box-sizing: border-box;
		background-color: #d53030;
	}

	.text-center {
		padding-top: 128rpx;
		height: 394rpx;
		box-sizing: border-box;
	}

	.zai-title {
		color: #fff;
		text-align: left;
		font-size: 62rpx;
		margin-left: 86rpx;
		line-height: 86.12rpx;
		/* margin-bottom: 36rpx; */
		font-family: "SourceHanSansSC-Medium", "SourceHanSansSC";
		font-weight: 500;
	}

	.login {
		width: 724rpx;
		height: 1230rpx;
		margin-top: 18rpx;
		/* padding-top: 168rpx; */
	}

	.login .login-count {
		position: absolute;
		top: 562rpx;
		left: 54rpx;
	}

	.login image {
		width: 724rpx;
		height: 100%;
	}

	.log_title {
		font-size: 64rpx;
		font-family: "SourceHanSansSC-Medium, SourceHanSansSC";
		font-weight: 500;
		color: #000000;
		line-height: 92rpx;
	}

	.u-form {
		padding-top: 60rpx !important;
	}

	.u-form .u-form-item {
		margin-bottom: 54rpx !important;
		padding-right: 56rpx !important;
	}

	.u-form .u-form-item.from_item2 {
		margin-bottom: 106rpx !;
	}

	.u-button {
		border: none;
		font-size: 30rpx;
		font-family: "SourceHanSansSC-Regular", "SourceHanSansSC";
		font-weight: 400;
		color: #000000;
		line-height: 42rpx;
	}

	.u-button.logins {
		width: 260rpx;
		height: 90rpx;
		background: #d53030;
		box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.1);
		border-top-left-radius: 49rpx;
		border-bottom-left-radius: 49rpx;
		font-size: 40rpx;
		font-family: "SourceHanSansSC-Regular", "SourceHanSansSC";
		font-weight: 400;
		color: #ffffff;
		line-height: 49rpx;
		margin-right: -26rpx;
		border: 0;
	}

	.logon_fu {
		width: 82.4%;
		font-size: 28rpx;
		font-family: "SourceHanSansSC-Regular", "SourceHanSansSC";
		font-weight: 400;
		color: #a4a4a4;
		line-height: 40rpx;
		margin: auto;
		text-align: center;
		margin-top: 140rpx;
		margin-bottom: 74rpx;
		margin-left: 0;
	}

	.logon_foot {
		font-size: 32rpx;
		font-family: "SourceHanSansSC-Regular", " SourceHanSansSC";
		font-weight: 400;
		color: #000000;
		line-height: 48rpx;
		text-align: center;
		transform: translateX(-54rpx);
	}

	.radius /deep/ .uni-radio-wrapper {
		width: 28rpx;
		height: 26rpx;
		margin-right: 20rpx;
	}

	uni-radio::before,
	uni-checkbox::before {
		font-family: "cuIcon";
		content: "\e645";
		position: absolute;
		color: #ffffff !important;
		top: 55%;
		margin-top: -8px;
		right: 11px;
		font-size: 10px;
		line-height: 16px;
		pointer-events: none;
		-webkit-transform: scale(1, 1);
		transform: scale(1, 1);
		transition: all 0.3s ease-in-out 0s;
		z-index: 9;
	}

	.radius /deep/ .uni-radio-wrapper .uni-radio-input {
		width: 28rpx;
		height: 26rpx;
	}
</style>